//
//  AGRequestNetwork.m
//  AGPastime
//
//  Created by Ager on 15/11/25.
//  Copyright © 2015年 Ager. All rights reserved.
//

#import "AGRequestNetwork.h"
#import <AFNetworking.h>

@implementation AGRequestNetwork

/**
 *  get 方法请求 xml
 */
+ (void)requestXML_GET:(NSString*)strUrl success:(void(^)(id XMLParser))success fail:(void(^)(id error))fail{
    
    AFHTTPRequestOperationManager *mamager = [AFHTTPRequestOperationManager manager];
    
   
    
    
    mamager.responseSerializer = [[AFXMLParserResponseSerializer alloc]init];
    [mamager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        fail(error);
    }];
}



/**
 *  post 方法请求 xml
 *
 * parameters 为 JSON 数据类型
 */
+ (void)requestXML_POST:(NSString*)strUrl parameters:(NSString*)parameters success:(void(^)(id XMLParser))success fail:(void(^)(id error))fail{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //设置请求格式
    manager.requestSerializer = [[AFJSONRequestSerializer alloc]init];
    //设置返回数据格式
    manager.responseSerializer = [[AFXMLParserResponseSerializer alloc]init];
    
    [manager POST:strUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        fail(error);
    }];
}

/**
 *  get 方法请求 json
 */
+ (void)requestJSON_GET:(NSString*)strUrl success:(void(^)(id json))success fail:(void(^)(id error))fail{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [[AFJSONResponseSerializer alloc]init];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",@"text/html",nil];
    
    [manager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        success(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        fail(error);
        
    }];
    
}



/**
 *  需要导入 UIKit+AFNetworking
 *
 *  @param strUrl     URL 地址
 *  @param parameters body 参数
 *  @param success    bolck 成功时调用
 *  @param fail       block 失败时调用
 */
+ (void)requestJSON_POST:(NSString*)strUrl parameters:(NSDictionary*)parameters success:(void(^)(id json))success fail:(void(^)(id error))fail{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //设置请求格式
    manager.requestSerializer = [[AFHTTPRequestSerializer alloc]init];
    //设置返回数据格式
    manager.responseSerializer = [[AFJSONResponseSerializer alloc]init];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",nil];
    
    
    [manager POST:strUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        fail(error);
    }];
}

+ (void)requestIMG_GET:(NSString*)strUrl success:(void(^)(id image))success fail:(void(^)(id error))fail{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [[AFImageResponseSerializer alloc]init];
    
    [manager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (fail) {
            fail(error);
        }
    }];
}


@end
